package org.javaboy.vhr.exception;

import org.javaboy.vhr.model.RespBean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局SQL异常捕获处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLException.class)
    public RespBean sqlException(SQLException e) {
        //如果该异常实例属于关联异常类及其子类
        if (e instanceof SQLIntegrityConstraintViolationException) {
            return RespBean.error("操作失败!该数据还有关联数据");
        }
        return RespBean.error("数据库异常，操作失败!");
    }
}